package com.sos.core.game
{
	import com.sos.utils.ClassUtils;

	public class GameEndCause
	{
			
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------              
		
		public static const PLAYER_WIN:GameEndCause				= new GameEndCause(true,  10, 	"PLAYER_WIN",			"Game won");
		public static const PLAYER_FLAWLESS_WIN:GameEndCause	= new GameEndCause(true,  11, 	"PLAYER_FLAWLESS_WIN",	"Game won with flawless victory");
		public static const PLAYER_LEAVE:GameEndCause			= new GameEndCause(false, 12, 	"PLAYER_LEAVE",			"Game lost, user leaved the game");
		public static const PLAYER_DISCREDITED:GameEndCause		= new GameEndCause(false, 13, 	"PLAYER_DISCREDITED",	"Game lost, too many turns passed");
		public static const OPPONENT_WIN:GameEndCause			= new GameEndCause(false, 20, 	"OPPONENT_WIN",			"Game lost, waiting for kick off");
		public static const OPPONENT_FLAWLESS_WIN:GameEndCause	= new GameEndCause(false, 21, 	"OPPONENT_FLAWLESS_WIN","Game lost, opponent flawless victory");
		public static const OPPONENT_LEAVE:GameEndCause			= new GameEndCause(true,  22, 	"OPPONENT_LEAVE",		"Game won, opponent leaved the game");
		public static const OPPONENT_DISCREDITED:GameEndCause	= new GameEndCause(true,  23, 	"OPPONENT_DISCREDITED",	"Game won, opponent passed too many turns");
		public static const OPPONENT_DISCONNECTS:GameEndCause	= new GameEndCause(true,  24, 	"OPPONENT_DISCONNECTS",	"User won, opponent disconnects");
		public static const DRAW:GameEndCause					= new GameEndCause(true,  30, 	"DRAW",					"Draw game");
		
		public static const CLIENT_ERROR:GameEndCause			= new GameEndCause(false, 90, 	"CLIENT_ERROR",			"CLIENT ERROR! Check game and server log!");
		
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<GameEndCause>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<GameEndCause>();
			_list.push(GameEndCause.PLAYER_WIN);
			_list.push(GameEndCause.PLAYER_FLAWLESS_WIN);
			_list.push(GameEndCause.PLAYER_LEAVE);
			_list.push(GameEndCause.PLAYER_DISCREDITED);
			_list.push(GameEndCause.OPPONENT_WIN);
			_list.push(GameEndCause.OPPONENT_FLAWLESS_WIN);
			_list.push(GameEndCause.OPPONENT_LEAVE);
			_list.push(GameEndCause.OPPONENT_DISCREDITED);
			_list.push(GameEndCause.OPPONENT_DISCONNECTS);
			_list.push(GameEndCause.DRAW);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		private var _description:String;
		private var _win:Boolean;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GameEndCause(win:Boolean, id:uint, name:String, desc:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("GameEndCause enum already created!");
			// Init
			_id    			= id;
			_name  			= name;
			_description	= desc;
			_win			= win;
		}

		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------              
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function get description():String
		{
			return _description;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names of 
		 * @return 
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
				
		/**
		 * Search a GameEndCause by given id 
		 * @param id Id of the card type to find
		 * @return PlayerGameEndCause constant for the given id, null if no GameEndCause
		 * for the given id has been found
		 */             
		public static function getById(id:uint):GameEndCause
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("GameEndCause for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a GameEndCause by given name 
		 * @param name Name of the player GameEndCause to find
		 * @return PlayerGameEndCause constant for the given id, null if no GameEndCause
		 * for the given name has been found
		 */             
		public static function getByName(name:String):GameEndCause
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("GameEndCause for name '" + name + "' unknown!");
		}
			
	}
}